﻿@page "/_School/School/Import"
@using WBBvNext.ViewModel._School.SchoolVMs;
@inherits BasePage

    <ValidateForm @ref="vform" Model="@Model" OnValidSubmit="@Submit">
        <Row ItemsPerRow="ItemsPerRow.Three" >
            <span style="font-size:18px;position:absolute;top:5px;"><Badge Color="Color.Info">1</Badge>@WtmBlazor.Localizer["Sys.ImportStep1"] </span>
            <Button type="button" Size="Size.Small" OnClick="DownloadTemplate" Text="@WtmBlazor.Localizer["Sys.DownloadTemplate"]" IsAsync="true"/>
        </Row>
        <Row ItemsPerRow="ItemsPerRow.Three">
            <span style="font-size:18px;position:absolute;top:30px;"><Badge Color="Color.Info">2</Badge>@WtmBlazor.Localizer["Sys.ImportStep2"] </span>
            <WTSimpleUpload @bind-Value="Model.UploadFileId" LabelText=""></WTSimpleUpload>
        </Row>
        <Row ItemsPerRow="ItemsPerRow.Two" style="@ErrorRowStyle">
            <span style="font-size:18px;position:absolute;top:5px;"><Badge Color="Color.Danger">3</Badge>@WtmBlazor.Localizer["Sys.ImportStep3"] </span>
            <Button type="button" Size="Size.Small" OnClick="DownloadErrorFile" Text="@WtmBlazor.Localizer["Sys.Download"]" IsAsync="true" />
        </Row>
        <div class="modal-footer table-modal-footer">
            <Button Color="Color.Primary" Icon="fa fa-save" Text="@WtmBlazor.Localizer["Sys.Close"]" OnClick="OnClose" />
            <Button Color="Color.Primary" ButtonType="ButtonType.Submit" Icon="fa fa-save" Text="@WtmBlazor.Localizer["Sys.Import"]" IsAsync="true"/>
        </div>
    </ValidateForm>

    @code {

        private SchoolImportVM Model = new SchoolImportVM() { };
        private ValidateForm vform { get; set; }
        private string ErrorRowStyle = "display:none";
        private string ErrorFileId;

        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            await base.OnAfterRenderAsync(firstRender);
        }

        private  async Task DownloadTemplate()
        {
            await Download("/api/School/GetExcelTemplate", new { }, HttpMethodEnum.GET);
        }

        private async Task Submit(EditContext context)
        {
            await PostsForm(vform, "/api/School/import",(s)=> WtmBlazor.Localizer["Sys.ImportSuccess",s],(err) => {
                ErrorRowStyle = "display:visiable";
                ErrorFileId = err.Form["Entity.ErrorFileId"];
            }) ;
        }

        private async Task DownloadErrorFile()
        {
            await Download($"/api/_file/DownloadFile/{ErrorFileId}", new { }, HttpMethodEnum.GET);
        }

        public void OnClose()
        {
            CloseDialog();
        }

    }
